package com.glory.hikvisionmobile.activity;


import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.hikvisionmobile.R;
import com.glory.hikvisionmobile.adapter.LotGoListAdapter;
import com.glory.hikvisionmobile.base.BaseActivity;
import com.glory.hikvisionmobile.base.Constant;
import com.glory.hikvisionmobile.bean.LotPauseOrGo.LotGoBean;
import com.glory.hikvisionmobile.bean.LotPauseOrGo.LotPauseListBean;
import com.glory.hikvisionmobile.bean.net.BaseResponseBean;
import com.glory.hikvisionmobile.utils.DialogUtils;
import com.glory.hikvisionmobile.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Lot_LotGoActivity extends BaseActivity {


    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.tvStepDetails)
    TextView tvStepDetails;
    @BindView(R.id.tvRecipe)
    TextView tvRecipe;
    @BindView(R.id.tvProductDetails)
    TextView tvProductDetails;
    @BindView(R.id.tvLotNumber)
    TextView tvLotNumber;
    @BindView(R.id.rvPauseList)
    RecyclerView rvPauseList;
    @BindView(R.id.tvReplay)
    TextView tvReplay;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.list_tv1)
    TextView listTv1;
    @BindView(R.id.list_tv2)
    TextView listTv2;
    @BindView(R.id.list_tv3)
    TextView listTv3;
    @BindView(R.id.list_tv4)
    TextView listTv4;
    @BindView(R.id.tvProductName)
    TextView tvProductName;
    @BindView(R.id.tvRecipeDesc)
    TextView tvRecipeDesc;
    @BindView(R.id.tvGoCode)
    TextView tvGoCode;
    @BindView(R.id.ivGoCodeListArrow)
    ImageView ivGoCodeListArrow;
    @BindView(R.id.flGoCodeList)
    FrameLayout flGoCodeList;
    @BindView(R.id.etRemarks)
    FreeUI_ClearEditText etRemarks;
    @BindView(R.id.etReason)
    FreeUI_ClearEditText etReason;
    private LotGoListAdapter lotGoListAdapter;
    private List<LotPauseListBean> lotPauseListBean;
    private FreeUI_GeneralPop codePop;
    private String goCode;
    private int selectCodePosition = -1;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_lot_go;
    }

    @Override
    protected void initView() {
        initListTitle();
        rvPauseList.setLayoutManager(new LinearLayoutManager(Lot_LotGoActivity.this));
        rvPauseList.setItemAnimator(new DefaultItemAnimator());
        rvPauseList.addItemDecoration(new DividerItemDecoration(Lot_LotGoActivity.this, 1));
        lotGoListAdapter = new LotGoListAdapter(null);
        lotGoListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        lotGoListAdapter.isFirstOnly(true);
        rvPauseList.setAdapter(lotGoListAdapter);
        initEdit(etLot, s -> requestData(s));

    }

    private void requestData(String lotId) {
        JsonObject map = new JsonObject();
        map.addProperty("lotId", lotId);
        NetUtils.requestDara("HIK.PdaLotRelease", this, map, new FreeUI_DialogEntityCallBack<BaseResponseBean<LotGoBean>>(
                new TypeToken<BaseResponseBean<LotGoBean>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<LotGoBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    LotGoBean lotGoBean = response.body().getBody();
                    tvLotNumber.setText(lotGoBean.getMainQty());
                    tvProductDetails.setText(lotGoBean.getPartDesc());
                    tvRecipe.setText(lotGoBean.getRecipeName());
                    tvStepDetails.setText(lotGoBean.getStepDesc());
                    tvRecipeDesc.setText(lotGoBean.getRecipeDesc());
                    tvProductName.setText(lotGoBean.getPartName());
                    lotPauseListBean = lotGoBean.getxPdaLotHolds();
                    lotGoListAdapter.setNewData(lotPauseListBean);
                    lotGoListAdapter.setSelectorPos(-1);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<LotGoBean>> response) {
                super.onError(response);
                loadError(response.getException(), "HIK.PdaTrackOutLoad");

            }
        });
    }


    @Override
    protected void initData() {
        NetUtils.getSelectorList("ReleaseCode","USER",this,getSupportFragmentManager(),new NetUtils.SelectorCallback(){

            @Override
            public void onSuccess(List<String> list) {
                if (codePop == null) {
                    codePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flGoCodeList.getWidth(), list, ivGoCodeListArrow, (codeS, i) -> {
                        tvGoCode.setText(codeS);
                        goCode = codeS.split("/")[0];
                        selectCodePosition = i;

                    });

                }
            }

            @Override
            public void onFail(Throwable e,String messageName) {
                loadError(e,messageName);
            }
        });
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("批次放行");
        freeUI_titleView.setRightListener("刷新", v -> {
            initPermission("Refresh", this, new ActionControlListener() {
                @Override
                public void onControlChangeListener() {
                    if (editIsEmpty(etLot, "批次号不能为空")) {
                        tvGoCode.setText("");
                        selectCodePosition = -1;
                        codePop = null;
                        goCode = "";
                        etRemarks.setText("");
                        etReason.setText("");
                        initData();
                        requestData(etLot.getText().toString().trim());
                    }
                }
            });
        });
        return new FreeUI_AddViewEntity("header", llMain);
    }


    @OnClick({R.id.tvReplay,R.id.flGoCodeList})
    public void onViewClicked(View v) {
        switch (v.getId()){
            case R.id.tvReplay:
                initPermission("Submit", this, new ActionControlListener() {
                    @Override
                    public void onControlChangeListener() {
                        submit();
                    }
                });
                break;
            case R.id.flGoCodeList:
                if (codePop == null) {
                    showToast("未请求到放行码列表", false);
                    etLot.setText("");
                    etLot.requestFocus();
                    return;
                }
                if (codePop.isShowing()) {
                    codePop.dismiss();
                } else {
                    FreeApi_PopUtils.setArrowShow(ivGoCodeListArrow);
                    codePop.showAsDropDown(flGoCodeList, 0, 0);
                }
                break;
        }

    }

    private void submit() {

        if (lotGoListAdapter.getSelectorPos() == -1) {
            showConfirmDialog("没有选择放行列表");
            return;
        }

        if (etLot.getText().toString().trim().length() == 0) {
            showToast("批次号不能为空", false);
            return;
        }

        if (selectCodePosition == -1){
            showToast("请选择放行码", false);
            return;
        }

        if (etReason.getText().toString().trim().length() == 0){
            showToast("请输入放行原因", false);
            return;
        }

        if (etRemarks.getText().toString().trim().length() == 0){
            showToast("请输入放行备注", false);
            return;
        }

        DialogUtils.showLoginDialog(this, Constant.CURRENT_USER, new DialogUtils.signCallback() {
            @Override
            public void onSignSuccess() {
                JsonObject map = new JsonObject();
                JsonObject json = new JsonObject();
                List<LotPauseListBean> jsonList = new ArrayList<>();
                LotPauseListBean lotPauseListBean = new LotPauseListBean();
                lotPauseListBean.setObjectRrn(lotGoListAdapter.getData().get(lotGoListAdapter.getSelectorPos()).getObjectRrn());
                jsonList.add(lotPauseListBean);
                json.add("XPdaLotHold", new JsonParser().parse(new Gson().toJson(jsonList)).getAsJsonArray());
                map.addProperty("lotId", etLot.getText().toString().trim());
                map.add("xPdaLotHolds", json);
                map.addProperty("actionType", "Off");
                map.addProperty("releaseReason",etReason.getText().toString().trim());
                map.addProperty("releaseComment",etRemarks.getText().toString().trim());
                map.addProperty("releaseCode",goCode);
                NetUtils.requestDara("HIK.PdaLotRelease", this, map, new FreeUI_DialogEntityCallBack<BaseResponseBean<LotGoBean>>(
                        new TypeToken<BaseResponseBean<LotGoBean>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<LotGoBean>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                            showConfirmDialog("批次放行成功");
                            etReason.setText("");
                            etRemarks.setText("");
                            requestData(etLot.getText().toString().trim());
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<LotGoBean>> response) {
                        super.onError(response);
                        loadError(response.getException(), "HIK.PdaTrackOutLoad");

                    }
                });
            }
        }, getSupportFragmentManager());
    }

    private void initListTitle() {
        listTv1.setText("暂停原因");
        listTv2.setText("暂停用户");
        listTv3.setText("暂停负责组");
        listTv4.setText("复选框");
        listTv1.setVisibility(View.VISIBLE);
        listTv2.setVisibility(View.VISIBLE);
        listTv3.setVisibility(View.VISIBLE);
        listTv4.setVisibility(View.VISIBLE);
    }
}
